package com.wans.guli.cart.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wans.guli.cart.service.CartService;
import com.wans.guli.cart.vo.CartItemVo;
import com.wans.guli.cart.vo.CartVo;
import com.wans.guli.cart.vo.MemberVO;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@Slf4j
public class WebViewController {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private CartService cartService;

    /**
     * 购物车页面
     *
     * @return 购物车页面
     */
    @GetMapping(value = {"/", "/index.html", "/cart.html", "/cartList.html"})
    @SneakyThrows
    public String index(Model model, HttpSession session) {


        CartVo cart = cartService.getCart();

        // 设置属性
        model.addAttribute("cart", cart);


        // 获取用户信息
        String loginMember = (String) session.getAttribute("loginMember");
        if (loginMember == null) {
            return "cartList";
        }
        MemberVO memberVO = objectMapper.readValue(loginMember, MemberVO.class);
        model.addAttribute("loginMember", memberVO);

        return "cartList";
    }


    @GetMapping(value = "/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes redirectAttributes) {

        // 添加商品到购物车
        CartItemVo cartItemVo = cartService.addCartItem(skuId, num);

        // 添加属性
        redirectAttributes.addAttribute("skuId", cartItemVo.getSkuId());

        // 重定向到成功页面
        return "redirect:http://cart.gulimall.com/success.html";
    }


    /**
     * 跳转到成功页面
     * @param skuId 商品skuId
     * @param model 设置属性
     * @param session  session
     * @return 成功页面
     */
    @GetMapping(value = "/success.html")
    @SneakyThrows
    public String success(@RequestParam("skuId") Long skuId, Model model, HttpSession session) {

        CartItemVo cartItem = cartService.getCartItemBySkuId(skuId);

        model.addAttribute("cartItem", cartItem);

        // 获取用户信息
        String loginMember = (String) session.getAttribute("loginMember");
        if (loginMember == null) {
            return "success";
        }
        MemberVO memberVO = objectMapper.readValue(loginMember, MemberVO.class);
        model.addAttribute("loginMember", memberVO);

        return "success";
    }

    /**
     * 修改购物车选中状态
     *
     * @param skuId   商品skuId
     * @param checked 选中状态
     * @return 重定向到购物车页面
     */
    @GetMapping(value = "/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer checked) {

        // 修改购物车选中状态
        cartService.updateCheckStatus(skuId, checked);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 修改购物车商品数量
     * @param skuId 商品skuId
     * @param num 商品数量
     * @return 重定向到购物车页面
     */
    @GetMapping(value = "/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {

        // 修改购物车商品数量
        cartService.updateCartSkuNum(skuId, num);

        return "redirect:http://cart.gulimall.com/cart.html";
    }

    /**
     * 删除购物车商品
     * @param skuId 商品skuId
     * @return 重定向到购物车页面
     */
    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        // 删除购物车商品
        cartService.removeById(skuId);

        return "redirect:http://cart.gulimall.com/cart.html";
    }
}
